<?php


/**
 * 前端资源功能
 * @package  htdocs
 * @subpackage  quanliadmin
 */

class pa_source {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] >= 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //资源列表
                    $this->sourceList();
                    break;

                case 2: //修改前端资源
                    $this->sourceEdit();
                    break;

                default:
                    backendError();
                    break;

            }

        } else {
            backendError();
        }

    }

    /**
     * 资源列表
     */
    private function sourceList() {

        global $G_SHOP;

        $G_SHOP->smarty->assign_by_ref('city_array', $G_SHOP->getCache('city'));

        $G_SHOP->smarty->display('content/sourceList.tpl');

    }

    /**
     * 修改前端资源
     */
    private function sourceEdit() {

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/content/source.class.php');

        if (!isset($_REQUEST['page']) || empty($_REQUEST['page'])) {
            backendError();
        }

        if (!isset($_REQUEST['cityid']) || empty($_REQUEST['cityid'])) {
            backendError();
        }

        //实例资源类
        $source = new source();

        //城市ID
        $cityID = $_REQUEST['cityid'];

        //页面URL
        $page_url = '';
        $page_type = '';
        $page_source = '';
        switch($_REQUEST['page']) {

            case 'index':
                $page_type = 1;
                $page_source = 'pc';
                $page_url = G_WEBSITE_DOMAINNAME . '/index.php?source_edit=1&subsite=shop&city_id=' . $cityID . '&act=' . echo_preview_url();
                break;

            case 'wap_index':
                $page_type = 2;
                $page_source = 'wap';
                $page_url = G_WEBSITE_DOMAINNAME . '/wap/index.php?source_edit=1&subsite=wap&city_id=' . $cityID . '&act=' . echo_preview_url();
                break;

            //add by wanglulu 20141212 新品推荐 start
            case 'wap_new_product':
                $page_type = 2;
                $page_source = 'wap';
                $page_url = G_WEBSITE_DOMAINNAME . '/wap/new_product.php?source_edit=1&subsite=wap&city_id=' . $cityID . '&act=' . echo_preview_url();
                break;
            //add by wanglulu 20141212 新品推荐 end

            default:
                backendError();
                break;

        }

        //查询已有的前端资源
        $source_array = $source->searchShopSource( $page_type, $cityID, $page_source);

        //抓取已有页面的html代码
        $html = file_get_contents( $page_url );

        $G_SHOP->smarty->assign('html', $html);
        $G_SHOP->smarty->assign('cityID', $cityID);
        $G_SHOP->smarty->assign('page_type', $page_type);
        if ($source_array) {
            $G_SHOP->smarty->assign_by_ref('source_array', json_encode($source_array));
        }

        $G_SHOP->smarty->display('content/source.tpl');

    }

}

